מבני בקרה ב C שעור מס. 2 דרור טובי דר' 1
פקודת if הוראה תנאי True (1) False (0) if ( grade >= 60 ) cout << You passed the test\n ; if ( grade >= 60 ) { cout << You passed the test\n ; passed = passed + 1; } 2
הערכים true ו false ביטוי שערכו אפס יחושב כ false ביטוי שערכו שונה מאפס יחושב כ true #include <iostream> int main() { int a=2; int b=3; if( a - b ) std::cout << "condition is true\n"; } return 0; 3
#include <iostream> using namespace std; משתנים בוליאנים int main() { /* boolean variable true if temp > 28 degrees Celsius */ bool is_hot = false; int temp; // temperature in Celsius cout << "Enter the temprature : "; cin >> temp; if( temp > 28 ) is_hot = true; if( is_hot ) std::cout << "It is hot today\n"; return 0; } tar2_1.cpp 4
פקודת if/else False (0) הוראה א תנאי הוראה ב True (1) if ( grade >= 60 ) cout << You passed the test\n ; else cout << You did not pass the test\n 5
משפטים מורכבים if ( grade >= 60 ) cout << Passed ; else { cout << Failed ; cout << You need to take the test again ; } if ( grade >= 60 ) cout << Passed ; else cout << Failed ; cout << You need to take the test again ; השווה טעויות לוגיות אינן מתגלות על ידי המהדר 6
תנאי if/else מקוננים if ( grade == 100 ) cout << Execelent ; else if( grade >= 90 ) cout << Very good ; else if( grade >= 80 ) cout << good ; else if( grade >= 60 ) cout << Passed ; else cout << Failed ; 7
float משתנים מטיפוס double ו משתנים מטיפוס double ו float משמשים לאחסון מספרים ממשיים. Type void int float double char Description associated with no data type integer floating-point number double precision floating-point number character Size (bytes) 0 4 4 8 1 int i = 2; float p1 = 3.14159; double p2 = 3.141592653589; char ch = 'A'; 8
חישוב שורש ריבועי 9 4 2 3 2?? כיצד נחשב שורש של 2 9
האלגוריתם הבבלי לחישוב שורש נרצה לחשב את השורש של c נניח ש t הוא השורש של c אזי מתקיים ש t=c/t 1) נתחיל בניחוש אקראי כל שהוא של מספר חיובי t 2) כל עוד (t-c/t) >ε כאשר הוא מספר חיובי קטן מאד (3 נבצע / 2.0 ) t t = ( c/t + התוצאה תתכנס לקבלת השורש הבבלים חיו במסופוטמיה עד 539 לפני בספירה )גבול סוריה/עירק( כל הזכויות שמורות דר' דרור טובי המרכז האוניברסיטאי אריאל 10
x 0 600.0 1 x ( x0 2 1 x ( x1 2 1 x ( x2 2 1 x ( x3 2 1 x ( x4 2 דוגמא Calculate, c where c = 125348 c 1 125348 ) (600.0 ) 2 600.0 404.457 c 1 125348 ) (404.457 ) 2 404.457 357.187 c 1 125348 ) (357.187 ) 2 357.187 354.059 c 1 125348 ) (354.059 ) x3 2 354.059 354.045 c 1 125348 ) (354.045 ) x 2 354.045 354.045 1 x0 2 x1 3 x2 4 5 4 125348 354.045 כל הזכויות שמורות דר' דרור טובי המרכז האוניברסיטאי אריאל 11
משפט החזרה while תנאי true משפטי ביצוע false int i =1; int sum = 0; ועד 5 לדוגמא סכימת המספרים מ 1 while ( i < 5 ) { sum = sum + i; i = i + 1; } 12
חישוב שורש // This program calculte sqrt of a number #include <iostream> #include <cmath> using namespace std; int main() { double c, t; double epsilon = 1e-6; cout << "Enter a naural number: "; cin >> c; t = c; // initial guess while( abs( t - c/t ) > epsilon ) t = 0.5*( c/t +t ); cout << "The root is : " << t << endl; return 0; } Enter a naural number: 9 The root is : 3 Enter a naural number: 125348 The root is : 354.045 13
דוגמא א לקריאת הקלט int counter=0; // count the number grades read int total=0; // total sum of grades int average=0; // average grade int passed=0; // number of students who passed int grade=0; // input grade... // reading data and calculating while( counter < 5 ) { cout << "Enter grade : "; cin >> grade; if( grade >= 60 ) passed = passed + 1; total = total + grade; counter = counter + 1; } average = total / counter;... tar2_2.cpp 14
בדיקת תקינות התוכנית השימוש המהדר compiler מאפשר לנו לזהות שגיאות תחביר אך לא שגיאות לוגיות נבצע בדיקה על ידי הרצה של דוגמאות להן אנו יודעים את התוצאה הצפויה הרצת בדיקה Enter grade : 23 Enter grade : 56 Enter grade : 78 Enter grade : 90 Enter grade : 77 The average grade is : 64 The number of students who passed the test is :3 15
משתנה מסוג float int counter; // count the number grades read int total; // total sum of grades float average; // average grade int passed; // number of students who passed int grade; // input grade.... // reading data and calculating while( counter < 5 ) { cout << "Enter grade : "; cin >> grade; if( grade >= 60 ) passed = passed + 1; total = total + grade; counter = counter + 1; } average = total / counter;.... float=int/int 16
Type Casting // implicit conversion average = total / counter; float = int / int average = 20 /3 = 6 average = float(total) / counter; float float int float float float promotion average = 20 /3 = 6.66 טעות לוגית average = float( total / counter) 17
casting int i = 6; float f = 8.5; // c type explicit conversion f = float(i); // f =6.0 f = (float) i; // f =6.0 i = int(f); // i=8 // C++ type conversion f = static_cast <float> (i); 18
סיכום casting //explicit casting (type) X type(x) static_cast <type> () (promotion) float במעבר מ int ל ישנו קידום ולכן ואין איבוד מידע int במעבר מ float ל ישנו איבוד מידע 19
דוגמא ג לקריאת הקלט וחישוב... // reading data and calculating while( grade!= -1 ) { cout << "Enter grade or -1 to end: "; cin >> grade; if( grade!= -1 ) { if( grade >= 60 ) passed = passed + 1; total = total + grade; counter = counter + 1; } } tar2_3.cpp // printing output average = static_cast <float> (total) / counter; cout << "The average grade is : " << average << endl;... 20
a = a + 3; a += 3; את ההוראה ניתן לכתוב גם הוראות השמה קיצור פסוק מלא פסוק מקוצר += a = a + b; a += b; -= a = a - b; a -= b; *= a = a * b; a *= b; /= a = a / b; a /= b; %= a = a % b; a %= b; 21
השמה דוגמאות int c=3, d=5, e=4, f=6, g=12; c += 6; d += f; e += 1; d -= 5; c -= g; e -= f; d *= 4; f *= c; g *= 2; f /= g; g /= f; d /= 3; g %= f; f %= e; d %= e; 22
הגדלה והקטנה למספר עוקב הגדלת הערך של a ב 1 ושימוש בערכו החדש ;a++ שימוש בערכו בנוכחי של a ואז הגדלת ערכו ב ;++a 1 הקטנת הערך של a ב 1 ושימוש בערכו בחדש ;a-- a--; שימוש בערכו בנוכחי של a ואז הקטנת ערכו ב 1 23
דוגמאות int a; a = 3; cout << a << endl; cout << ++a << endl; cout << a++ << endl; cout << a << endl a = 3; cout << a << endl; cout << --a << endl; cout << a-- << endl; cout << a << endl; int a; a = 3; ++a; cout << a << endl; a=3; a++; cout << a << endl; 24
לולאת ה for i = 1 תנאי true cout << i ++i false שינוי משתנה הלולאה תנאי הלולאה איתחול משתנה הלולאה for( int i=1; i<=10; ++i) cout << i << endl; for( int i=1; i<=10; i+=2) cout << i << endl; השווה 25
לולאת ה for המשך for( int i=10; i>1; --i) cout << i << endl; השוה for( int i=1; i<=10; --i) cout << i << endl; טעות לוגית הגורמת ללולאה אין סופית 26
דוגמא ללולאת for #include <iostream> using namespace std; int main() { int sum = 0; for(int i=2; i<=100; i+=2) sum += i; cout << "Sum is : " << sum << endl; } return 0; tar2_4.cpp 27
משתנה מסוג char char ch; ch = A #include <iostream> int main() { for( char ch = 'A'; ch <='Z'; ++ch) std::cout << ch << std::endl; } return 0; 28
Char and Int #include <iostream> int main() { for( char ch = 'A'; ch <='Z'; ++ch) std::cout << ch << ' ' << int(ch) << std::endl; return 0; } tar2_6.cpp 29
OUTPUT ASCII Table A 65 B 66 C 67 D 68 E 69 F 70 G 71 H 72 I 73 J 74 K 75 L 76 M 77 N 78 O 79 P 80 Q 81 R 82 S 83 T 84 U 85 דרור טובי, המרכז האוניברסיטאי אריאל בשומרון. Char Dec Oct Hex ------------------- @ 64 0100 0x40 A 65 0101 0x41 B 66 0102 0x42 C 67 0103 0x43 D 68 0104 0x44 E 69 0105 0x45 F 70 0106 0x46 G 71 0107 0x47 H 72 0110 0x48 I 73 0111 0x49 J 74 0112 0x4a K 75 0113 0x4b L 76 0114 0x4c M 77 0115 0x4d N 78 0116 0x4e O 79 0117 0x4f P 80 0120 0x50 Q 81 0121 0x51 דר' R 82 0122 0x52 30
משתנים סיכום char Character or small integer. 1byte short int (short) Short Integer. 2bytes int Integer. 4bytes long int (long) Long integer. 4bytes signed: -128 to 127 unsigned: 0 to 255 signed: -32768 to 32767 unsigned: 0 to 65535 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 bool Boolean value. It can take one of two values: true or false. 1byte true or false float Floating point number. 4bytes 3.4e +/- 38 (7 digits) double long double Double precision floating point number. Long double precision floating point number. 8bytes 8bytes 1.7e +/- 308 (15 digits) 1.7e +/- 308 (15 digits) 31
לולאת do/while do { statement statement; true } while (condition); תנאי false 32
דוגמא א #include <iostream> using namespace std; int main() { int counter = 1; } do { cout << counter << endl; ++counter; } while ( counter <= 10 ); return 0; הפלט 1 2 3 4 5 6 7 8 9 10 tar2_7.cpp 33
דוגמא ב #include <iostream> using namespace std; int main() { int counter = 20; } do { cout << counter << endl; ++counter; } while ( counter <= 10 ); return 0; הפלט 20 tar2_8.cpp 34
פקודת Break #include <iostream> using namespace std; int main() { פקודת break מביאה לסיום ויציאה מידית מהלולאה } for(int i=1; i<=5; ++i) { cout << i << endl; if( i == 3 ) break; } return 0; tar2_9.cpp 1 2 3 35
פקודת continue #include <iostream> using namespace std; int main() { } for(int i=1; i<=5; ++i) { if( i == 3 ) continue; cout << i << endl; } return 0; tar2_10.cpp 1 2 4 5 36
== או = If ( bonuscode == 4 ) cout << You get a bonus!!! << end; If (bonuscode = 4 ) cout << You get a bonus!!! << end; 37
מילות שמורות מילים שמורות לשפת ++C C / auto const double float int short struct unsigned break continue else for long signed switch void case default enum goto register sizeof typedef volatile char do extern if return static union while מילים שמורות לשפת ++C בלבד asm dynamic_cast namespace reinterpret_cast try bool explicit new static_cast typeid catch false operator template typename class friend private this using const_cast inline public throw virtual delete mutable protected true wchar_t 38